//
//  HMEmoticonCollectionViewCell.swift
//  MicroBlog
//
//  Created by Apple on 16/10/6.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
//自定义表情视图cell
class HMEmoticonCollectionViewCell: UICollectionViewCell {
    //记录添加的按钮
    lazy var emoticonButtonArray: [HMEmoticonButton] = [HMEmoticonButton]()
    
    //准备cell需要的数据源
    var emoticonsArray: [HMEmoticon]? {
        didSet {
            guard let entArray = emoticonsArray else {
                return
            }
            //把表情按钮的元素全部隐藏
            for button in emoticonButtonArray {
                button.isHidden = true
            }
            //代码执行到此,说明数组不为nil
            for (i,emoticon) in entArray.enumerated() {
                //获取表情按钮
                let button = emoticonButtonArray[i]
                //设置表情模型
                button.emoticon = emoticon
            }
        }
    }
    
    //添加删除按钮
    fileprivate lazy var deleteButton: UIButton = {
       let button = UIButton()
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        button.addTarget(self, action: #selector(deleteBtnAction(btn:)), for: .touchUpInside)
        return button
    }()
    
    //删除按钮点击事件
    @objc private func deleteBtnAction(btn: UIButton) {
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedDeleteEmoticonNotification), object: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addChildEmoticonButton()
        addSubview(deleteButton)
    }
    
    //添加表情按钮
    func addChildEmoticonButton() {
        for _ in 0..<20 {
            let button = HMEmoticonButton()
            button.addTarget(self, action: #selector(emoticonButtonAction(btn:)), for: .touchUpInside)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            contentView.addSubview(button)
            emoticonButtonArray.append(button)
        }
    }
    
    //点击表情按钮事件
    @objc private func emoticonButtonAction(btn: HMEmoticonButton) {
        
        let emoticon = btn.emoticon!
        //
        NotificationCenter.default.post(name: Notification.Name(DidSelectedEmoticonNotification), object: emoticon)
        let popView = HMEmoticonPopView.emoticonPopView()
        //设置PopView的表情类型
        popView.emoticonButton.emoticon = emoticon
        //获取window
        let window = UIApplication.shared.windows.last!
        window.addSubview(popView)
        //Btn在表情视图上的坐标转化成window上的坐标
        let convertRect = contentView.convert(btn.frame, to: window)
        //获取这个范围内的水平中心点x
        popView.centerX = convertRect.midX
        popView.y = convertRect.midY - popView.height
        //延时移除popView
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.25) { 
            popView.removeFromSuperview()
        }
    }
    
    //设置子控件大小和位置
    override func layoutSubviews() {
        super.layoutSubviews()
        //宽度
        let itemWidth = width / 7
        //高度
        let itemHeight = height / 3
        //遍历表情数组
        for (i,button) in emoticonButtonArray.enumerated() {
            button.size = CGSize(width: itemWidth, height: itemHeight)
            //列号
            let col = i % 7
            //行号
            let row = i / 7
            //设置x,y
            button.x = CGFloat(col) * itemWidth
            button.y = CGFloat(row) * itemHeight
        }
        
        //设置删除按钮的大小和坐标
        deleteButton.size = CGSize(width: itemWidth, height: itemHeight)
        deleteButton.x = width - itemWidth
        deleteButton.y = height - itemHeight
    }
}
